#include<bits/stdc++.h>
using namespace std;
inline long long read(){
	long long ret=0,neg=1;
	char ch;
	ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
	if(ch=='-'){
		neg=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		ret=(ret<<1)+(ret<<3)+(ch^48);
		ch=getchar();
	}
	return ret*neg;
}
void write(long long x){
	if(x<0) putchar('-'),x=-x;
	if(x>=10) write(x/10);
	putchar('0'+x%10);
}

const long long mod=1000000007;

long long f[1010][1010],g[1010][1010];
int n;
int p[2][1010],pos[2][1010];

long long dfs(int i,int j);

long long Protogen(int i,int j){
	if(g[i][j]!=-1) return g[i][j];
	if(i==n&&j==n){
		g[i][j]=1;
		return 1;
	}
	g[i][j]=0;
	int x=pos[1][p[0][i]];
	int y=i+j-x;
	if(i==y&&j==x){
		g[i][j]=dfs(i,j);
		return g[i][j];
	}
	if(j<n&&y<n) g[i][j]+=Protogen(i,j+1);
	if(i<n&&x<n&&p[1][j]==p[0][y]) g[i][j]+=Protogen(y,x+1);
	g[i][j]%=mod;
	// cout<<i<<' '<<j<<' '<<g[i][j]<<endl;
	return g[i][j];
}

long long dfs(int i,int j){
	if(f[i][j]!=-1) return f[i][j];
	if(i==n&&j==n){
		f[i][j]=1;
		return 1;
	}
	f[i][j]=0;
	if(i<n) f[i][j]+=dfs(i+1,j);
	if(j<n) f[i][j]+=dfs(i,j+1);
	if(i<n&&j<n&&p[0][i]==p[1][j]){
		f[i][j]+=mod-Protogen(i,j+1);
	}
	f[i][j]%=mod;
	// cout<<i<<' '<<j<<' '<<f[i][j]<<endl;
	return f[i][j];
}

int main(){
	freopen("zha.in","r",stdin);
	freopen("zha.out","w",stdout);
	n=read();
	for(int i=1;i<=n;++i){
		p[0][i]=read();
		pos[0][p[0][i]]=i;
	}
	for(int i=1;i<=n;++i){
		p[1][i]=read();
		pos[1][p[1][i]]=i;
	}
	for(int i=0;i<=n;++i){
		for(int j=0;j<=n;++j){
			f[i][j]=g[i][j]=-1;
		}
	}
	write(dfs(1,1));
	return 0;
}